/*
 * @lc app=leetcode.cn id=135 lang=swift
 *
 * [135] 分发糖果
 */

// @lc code=start
class Solution {
    func candy(_ ratings: [Int]) -> Int {
        var candys: [Int] = Array(repeating: 1, count: ratings.count)
        for i in 1..<ratings.count {
            if ratings[i] > ratings[i-1] {
                candys[i] = candys[i-1] + 1
            }
        }
        for i in (1..<ratings.count).reversed() {
            if ratings[i] < ratings[i-1] {
                candys[i-1] = max(candys[i-1], candys[i] + 1)
            }
        }

        return candys.reduce(0, +)
    }
}
// @lc code=end

